class IndexedDB {
    db = null;

    constructor() {
        this.init()
    }

    getTable(name) {
        return this.db.objectStoreNames.contains(name)
    }

    init() {
        const currentDB = window.indexedDB.open('webDB',new Date().getTime());
        currentDB.addEventListener('error', () => {
            console.error('错误')
        })
        currentDB.addEventListener('success', () => {
            // debugger;
            this.db = currentDB.result
            // console.log(this.db)

        })
        currentDB.addEventListener('upgradeneeded', (event) => {
            this.db = event.target.result;
            this.initConfigTable()
            // debugger;

        })
    }

    initConfigTable() {
        if (!this.getTable('configTable')) {
            const object = this.db.createObjectStore('configTable', {keyPath: 'id', autoIncrement: true});
            object.createIndex('id', 'id', {unique: true})
            object.createIndex('config', 'config', {unique: false})
        }
    }

}

export default new IndexedDB()
